/**
 * Created by dell、 on 2017/7/11.
 */
    //全局变量
var item3 = [];
var data3 = [];
var item2 = [];
var data2 = [];
var data1 = [];
var item1 = [];

function countChannelBorrow(param) {
    //第一个ajax 渠道对应标的的总金额以及占标的库总金额的百分比
    $.ajax({
        type: 'POST',
        url: "/echarts/countChannelBorrow/1",
        data: JSON.stringify(param),
        dataType: "json",
        contentType: "application/json",
        success: function (data) {
            console.log("返回数据：" + data);
            console.log("返回数据 data 长度" + data.length);
            for (var i = 0; i < data.length; i++) {
                if (typeof(data[i]["name"]) == "undefined" || data[i]["name"] == null || data[i]["name"] == "") {
                    // console.log(data[i].value + " " + i)
                    data1.push({"value": data[i].sum, "name": "名称为空"});
                    // console.log(data1[i])
                    item1.push("名称为空");
                } else {
                    // console.log(data[i].sum + " " + i)
                    data1.push({"value": data[i].sum, "name": data[i].name});
                    // console.log(data1[i])
                    item1.push(data[i].name);
                    // console.log("data[i].name" + data[i].name)
                    // console.log(item1[i])
                }
            }
            // console.log("放入数组后的：tem:格式化为json后" + JSON.stringify(data1));
            // console.log(item1);
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('main'));
            // 指定图表的配置项和数据
            var option = {
                title: {
                    text: '资产管理后台标的库',
                    subtext: '渠道对应标的总金额及占标的库总金额的百分比',
                    x: 'left'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {
                            show: true,
                            type: ['pie', 'funnel'],
                            option: {
                                funnel: {
                                    x: '25%',
                                    width: '50%',
                                    funnelAlign: 'left',
                                    max: 1548
                                }
                            }
                        },
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    y: 50,
                    data: item1
                },
                series: [
                    {
                        name: '渠道对应的标的总金额（元）',
                        type: 'pie',
                        radius: '75%',
                        center: ['50%', '55%'],
                        data: data1,
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
        }
    });
}
function countChannelBorrow2(params) {
    //第二个ajax 标的库的售卖情况的统计图
    $.ajax({
        type: 'POST',
        url: "/echarts/countChannelBorrow/2",
        data: JSON.stringify(params),
        dataType: "json",
        contentType: "application/json",
        success: function (data) {
            console.log("返回数据：" + data);
            console.log("返回数据 data 长度" + data.length);
            for (var i = 0; i < data.length; i++) {
                if (typeof(data[i]["name"]) == "undefined" || data[i]["name"] == null || data[i]["name"] == "") {
                    // console.log(data[i].value + " " + i)
                    data2.push({"value": data[i].value, "name": "名称为空"});
                    // console.log(data2[i])
                    item2.push("名称为空");
                } else {
                    // console.log(data[i].value + " " + i)
                    data2.push({"value": data[i].value, "name": data[i].name});
                    // console.log(data2[i])
                    item2.push(data[i].name);
                    // console.log("data[i].name" + data[i].name)
                    // console.log(item2[i])
                }
            }
            // console.log("放入数组后的：tem:格式化为json后" + JSON.stringify(data2));
            // console.log(item2);
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('main2'));
            // 指定图表的配置项和数据
            var option = {
                title: {
                    text: '资产管理后台标的库',
                    subtext: '标的的售卖情况统计',
                    x: 'left'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {
                            show: true,
                            type: ['pie', 'funnel'],
                            option: {
                                funnel: {
                                    x: '25%',
                                    width: '50%',
                                    funnelAlign: 'left',
                                    max: 1548
                                }
                            }
                        },
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    y: 50,
                    data: item2
                },
                series: [
                    {
                        name: '标的数及所占比例',
                        type: 'pie',
                        radius: '75%',
                        center: ['50%', '55%'],
                        data: data2,
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
        }
    });
}

function countThirdPartyBorrow(params) {
    //第三个ajax 统计第三方来标成功失败图
    $.ajax({
        type: 'POST',
        url: "/echarts/countThirdPartyBorrow",
        data: params,
//            dataType: "json",
        success: function (data) {
            console.log("返回数据：" + data);
            console.log("返回数据 total  " + data.total + "  successTotal: " + data.successTotal);
            item3.push("成功笔数");
            item3.push("失败笔数");
            data3.push({"value": data.successTotal, "name": "成功笔数"});
            data3.push({"value": (data.total - data.successTotal), "name": "失败笔数"});
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('main3'));
            // 指定图表的配置项和数据
            var option = {
                title: {
                    text: '资产管理后台标的库',
                    subtext: '第三方来标成功失败统计图',
                    x: 'left',
                    y: 0
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {
                            show: true,
                            type: ['pie', 'funnel'],
                            option: {
                                funnel: {
                                    x: '25%',
                                    width: '50%',
                                    funnelAlign: 'left',
                                    max: 1548
                                }
                            }
                        },
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    y: 50,
                    data: item3
                },
                series: [
                    {
                        name: '第三方来标',
                        type: 'pie',
                        radius: '75%',
                        center: ['50%', '55%'],
                        data: data3,
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
        }
    })
}

function search() {
    item1.length = 0;
    data1.length = 0;
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    // console.log(startTime)
    // console.log(endTime)
    var param = {startTime: startTime, endTime: endTime};
    countChannelBorrow(param);
}

function search2() {
    item2.length = 0;
    data2.length = 0;
    var startTime2 = $("#startTime2").val();
    var endTime2 = $("#endTime2").val();
    var options = $("#channelInfo option:selected");//获取选中的项
    // alert(options.val());
    var channelId = options.val();
    var param = {startTime: startTime2, endTime: endTime2, channelId: channelId};
    countChannelBorrow2(param);
}



